package dfs_bfs.test46;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

/**
 * 全排列
 *
 * @author code_yc
 * @version 1.0
 * @date 2020/7/9 9:27
 */
public class Solution1 {

    List<List<Integer>> res = new ArrayList<>();
    boolean[] isUsed;
    int len;

    public List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0) return res;
        len = nums.length;
        isUsed = new boolean[len];
        dfs(nums, new ArrayDeque<Integer>());
        return res;
    }

    private void dfs(int[] nums, ArrayDeque<Integer> path) {
        if (path.size() == len) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < len; i++) {
            if (!isUsed[i]) {
                isUsed[i] = true;
                path.addLast(nums[i]);
                dfs(nums, path);
                path.pollLast();
                isUsed[i] = false;
            }
        }
    }
}
